package com.kaiwukj.android.ufamily.mvp.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.kaiwukj.android.ufamily.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Charles
 * @date 2019/8/14.
 * description：Kaiwu
 */
public class UploadAdapter extends RecyclerView.Adapter {
    public static final int IMG = 1, ADD = 2, AUDIO = 3, VIDEO = 4;
    private Context context;
    private List<String> imgUrls = new ArrayList<>();
    private String videoUrl;
    private int maxImgs;

    private boolean haveAudio;

    //    1 可编辑 2 仅展示
    private int adapterType = EDITTYPE;

    public UploadAdapter(Context context) {
        this.context = context;
    }

    private UploadAdapter() {
    }

    public UploadAdapter(Context context, int adapterType) {
        this.context = context;
        this.adapterType = adapterType;
    }

    public void setMaxImgs(int maxImgs) {
        this.maxImgs = maxImgs;
    }

    public void setData(List<String> imgUrls) {
        this.imgUrls = imgUrls;
        notifyDataSetChanged();
    }

    public void addData(List<String> data) {
        if (data != null & data.size() > 0)
            imgUrls.addAll(data);
        notifyDataSetChanged();
    }

    public List<String> getData() {
        return imgUrls;
    }

    public void removeImg(int position) {
        imgUrls.remove(position);
        notifyDataSetChanged();
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
        notifyDataSetChanged();
    }

    public void setHaveAudio(boolean haveAudio) {
        this.haveAudio = haveAudio;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        if (viewType == IMG) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_upload_img, parent, false);
            return new ImgHolder(view);
        } else if (viewType == ADD) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_upload_img_add, parent, false);
            return new AddImgHolder(view);
        } else if (viewType == VIDEO) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_upload_video, parent, false);
            return new VideoHolder(view);
        } else if (viewType == AUDIO) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_upload_audio, parent, false);
            return new AudioHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == IMG) {
            ImgHolder imgHolder = (ImgHolder) holder;
            imgHolder.ivDeleteImg.setVisibility(adapterType == EDITTYPE ? View.VISIBLE : View.GONE);
            if (imgUrls.size() > 0) {
                Glide.with(context).load(imgUrls.get(haveAudio ? position : position - 1)).into(imgHolder.ivImage);
            }
            imgHolder.ivImage.setOnClickListener(view -> {
                if (clickListener == null) {
                    return;
                }
                clickListener.onImgClick(imgUrls, haveAudio ? position : position - 1);
            });
            imgHolder.ivDeleteImg.setOnClickListener(view -> {
                if (clickListener == null) {
                    return;
                }
                clickListener.onDelImgClick(haveAudio ? position : position - 1);
            });
        } else if (getItemViewType(position) == ADD) {
            AddImgHolder addImgHolder = (AddImgHolder) holder;
            addImgHolder.tvAddText.setText(imgUrls.size() > 0 ? imgUrls.size() + "/" + maxImgs : "添加图片");
            addImgHolder.ivAddImg.setOnClickListener(view -> {
                if (clickListener == null) {
                    return;
                }
                clickListener.onAddClick();
            });
        } else if (getItemViewType(position) == AUDIO) {
            AudioHolder audioHolder = (AudioHolder) holder;
//            audioHolder.ivAudio.setOnTouchListener((view, motionEvent) -> {
//                switch (motionEvent.getAction()){
//                    case MotionEvent.ACTION_DOWN:
//                        clickListener.onAudioClick();
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        clickListener.onAudioStopClick();
//                        break;
//                }
//                return true;
//            });
            audioHolder.ivAudio.setOnClickListener(view -> {
                if (clickListener == null) {
                    return;
                }
                clickListener.onAudioClick();
            });
        } else if (getItemViewType(position) == VIDEO) {
            VideoHolder videoHolder = (VideoHolder) holder;
            if (!TextUtils.isEmpty(videoUrl)) {
                Glide.with(context).load(videoUrl).into(videoHolder.ivVideo);
                videoHolder.ivDeleteVideo.setVisibility(adapterType == SHOWTYPE ? View.GONE : View.VISIBLE);
                videoHolder.ivPlayVideo.setVisibility(View.VISIBLE);
            } else {
                Glide.with(context).load(R.mipmap.icon_upload_video).into(videoHolder.ivVideo);
                videoHolder.ivDeleteVideo.setVisibility(View.GONE);
                videoHolder.ivPlayVideo.setVisibility(View.GONE);
            }
            videoHolder.ivVideo.setOnClickListener(view -> {
                if (clickListener == null) {
                    return;
                }
                if (!TextUtils.isEmpty(videoUrl)) {
                    clickListener.onShowVideoClick(videoUrl);
                } else {
                    clickListener.onVideoClick();
                }
            });
            videoHolder.ivDeleteVideo.setOnClickListener(view -> {
                if (clickListener == null) {
                    return;
                }
                clickListener.onDeleteVideoClick();
            });
        }
    }

    static class AddImgHolder extends RecyclerView.ViewHolder {
        ImageView ivAddImg;
        TextView tvAddText;

        public AddImgHolder(@NonNull View itemView) {
            super(itemView);
            ivAddImg = itemView.findViewById(R.id.iv_add);
            tvAddText = itemView.findViewById(R.id.tv_add_text);
        }
    }

    static class ImgHolder extends RecyclerView.ViewHolder {
        ImageView ivImage;
        ImageView ivDeleteImg;

        public ImgHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_image);
            ivDeleteImg = itemView.findViewById(R.id.iv_delete_img);
        }
    }

    static class VideoHolder extends RecyclerView.ViewHolder {
        ImageView ivVideo;
        ImageView ivDeleteVideo;
        ImageView ivPlayVideo;

        public VideoHolder(@NonNull View itemView) {
            super(itemView);
            ivVideo = itemView.findViewById(R.id.iv_video);
            ivDeleteVideo = itemView.findViewById(R.id.iv_delete_video);
            ivPlayVideo = itemView.findViewById(R.id.iv_play_video);
        }
    }

    static class AudioHolder extends RecyclerView.ViewHolder {
        ImageView ivAudio;

        public AudioHolder(@NonNull View itemView) {
            super(itemView);
            ivAudio = itemView.findViewById(R.id.iv_audio);
        }
    }

    public interface onItemClickListener {

        void onAudioClick();

        void onVideoClick();

        void onShowVideoClick(String coverUrl);

        void onDeleteVideoClick();

        void onAddClick();

        void onDelImgClick(int position);

        void onImgClick(List<String> imgUrls, int position);
    }

    onItemClickListener clickListener;

    public void setOnItemClickListener(onItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public int getItemCount() {
        if (adapterType == SHOWTYPE) {
            return imgUrls.size() + (haveAudio ? 0 : 1) + (TextUtils.isEmpty(videoUrl) ? 0 : 1);
        } else {
            if (imgUrls.size() == maxImgs) {
                return imgUrls.size() + (haveAudio ? 1 : 2);
            }
            return imgUrls.size() + (haveAudio ? 2 : 3);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (adapterType == SHOWTYPE) {
            if (position == 0) {
                return imgUrls.size() > 0 ? IMG : VIDEO;
            } else {
                if (TextUtils.isEmpty(videoUrl)) {
                    return IMG;
                } else {
                    return position < imgUrls.size() ? IMG : VIDEO;
                }
            }
        } else {
            if (position == 0) {
                if (haveAudio) {
                    return imgUrls.size() > 0 ? IMG : ADD;
                } else {
                    return AUDIO;
                }
            } else {
                if (imgUrls.size() == maxImgs) {
                    return position < (haveAudio ? maxImgs : maxImgs + 1) ? IMG : VIDEO;
                } else {
                    if (position == imgUrls.size() + (haveAudio ? 0 : 1)) {
                        return ADD;
                    } else {
                        return position < imgUrls.size() + (haveAudio ? 0 : 1) ? IMG : VIDEO;
                    }
                }
            }
        }
    }

    public static int EDITTYPE = 1;
    public static int SHOWTYPE = 2;
}
